<?php
Prado::using('Application.common.common');
class AddFoodPlace extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = "Admin | Add Food Place";
			$this->AdminMenu->lnkAddFoodPlace->CssClass = "active";
			
			// Populate MRT List			
			$list = array_reverse(MRTRecord::finder()->getAllItems());
			$list[] = array("Name" => "Not Sure", "ID" => -1);
			$list = array_reverse($list);
			$this->cboMRTSelector->DataSource = $list;//MRTRecord::finder()->getAllItems();
			$this->cboMRTSelector->DataBind();
			
			// Populate Price Range List
			$this->cboPriceRangeSelector->DataSource = PriceRangeRecord::finder()->getAllItems();
			$this->cboPriceRangeSelector->DataBind();
			
            // Populate Close Time List
			$this->cboCloseTimeSelector->DataSource = CloseTimeRecord::finder()->getAllItems();
            $this->cboCloseTimeSelector->DataBind();
			
			// Populate Cuisine List
			$this->cboCuisine1Selector->DataSource = $this->cboCuisine2Selector->DataSource = $this->cboCuisine3Selector->DataSource =  CuisineRecord::finder()->getAllItems();
			$this->cboCuisine1Selector->DataBind();
			$this->cboCuisine2Selector->DataBind();
			$this->cboCuisine3Selector->DataBind();
		}
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{						
			try {
				$user = UserRecord::finder()->findByPk($this->Application->User->ID);
				if ($user instanceof UserRecord)
				{						
					$foodPlaceRecord = new FoodPlaceRecord;						
					$foodPlaceRecord->Name = $this->txtName->SafeText;
					$foodPlaceRecord->Highlights = $this->txtHighlights->SafeText;					
					$foodPlaceRecord->NearestMRT = $this->cboMRTSelector->SelectedValue;
					$foodPlaceRecord->PriceRange = $this->cboPriceRangeSelector->SelectedValue;
					$foodPlaceRecord->OpenUntil = $this->cboCloseTimeSelector->SelectedValue;										
					$foodPlaceRecord->Location = $this->txtLocation->SafeText;					
					$foodPlaceRecord->OperatingHours = $this->txtOperatingHours->SafeText;					
					$foodPlaceRecord->Contact = $this->txtContact->SafeText;
					$foodPlaceRecord->PlaceDetails = $this->txtDetails->SafeText;
					$foodPlaceRecord->MapGoogle = $this->txtMapGoogle->Text;
					$foodPlaceRecord->Status = "1";
					$foodPlaceRecord->IsRecommended = "0";
					if ($this->chkRecommendedFoodPlace->Checked)	
						$foodPlaceRecord->IsRecommended = "1";
					$foodPlaceRecord->IsNew = "0";
					if ($this->chkNewFoodPlace->Checked)	
						$foodPlaceRecord->IsNew = "1";	
					$foodPlaceRecord->CreateBy = $foodPlaceRecord->ModifyBy = $user->ID;
					$foodPlaceRecord->CreateDate = $foodPlaceRecord->ModifyDate = time();
					
					$hashImage = "";
					$currentYear = date("Y");
					$currentMonth = date("m");
					$filePath = dirname($this->Request->ApplicationFilePath).DIRECTORY_SEPARATOR."useruploads".DIRECTORY_SEPARATOR."photos".DIRECTORY_SEPARATOR."food-place".DIRECTORY_SEPARATOR.$currentYear.DIRECTORY_SEPARATOR;
					Common::check_dir($filePath);
					$filePath = $filePath.$currentMonth.DIRECTORY_SEPARATOR;				
					Common::check_dir($filePath);
					
					if($this->fuMainPhoto->HasFile) 
					{			
						$hashStr=md5(uniqid(time()));
						$imageExt=strtolower(array_pop(explode('.',$this->fuMainPhoto->FileName)));
						$hashImage=$hashStr.'.'.$imageExt;	
						$source = $filePath.$hashStr.$this->Application->Parameters['AFFFIX_ORG'].'.'.$imageExt;
						$this->fuMainPhoto->saveAs($source, true);
						$foodPlaceRecord->MainPhoto=$hashImage;
																	
						Common::saveFoodPlaceMainPhoto($source, $filePath, $hashImage, $hashStr, $imageExt);	
					}
										
					$foodPlaceRecord->save();
					
					// create photo 0
					$photoRecord = new PhotoRecord;					
					$photoRecord->PhotoFile = $hashImage;
					$photoRecord->FoodPlaceID = $foodPlaceRecord->ID;
					$photoRecord->SortOrder = 0;
					$photoRecord->CreateBy = $user->ID;
					$photoRecord->CreateDate = time();			
					$photoRecord->save();
					
					if($this->fuPhoto1->HasFile) 
					{
						Common::savePhoto($this->fuPhoto1, $filePath, 1, $foodPlaceRecord->ID, $user->ID);						
					}	
					
					if($this->fuPhoto2->HasFile) 
					{
						Common::savePhoto($this->fuPhoto2, $filePath, 2, $foodPlaceRecord->ID, $user->ID);						
					}
					
					if($this->fuPhoto3->HasFile) 
					{
						Common::savePhoto($this->fuPhoto3, $filePath, 3, $foodPlaceRecord->ID, $user->ID);						
					}
					
					if($this->fuPhoto4->HasFile) 
					{
						Common::savePhoto($this->fuPhoto4, $filePath, 4, $foodPlaceRecord->ID, $user->ID);						
					}
					
					if($this->fuPhoto5->HasFile) 
					{
						Common::savePhoto($this->fuPhoto5, $filePath, 5, $foodPlaceRecord->ID, $user->ID);						
					}
					
					if($this->fuPhoto6->HasFile) 
					{
						Common::savePhoto($this->fuPhoto6, $filePath, 6, $foodPlaceRecord->ID, $user->ID);						
					}
					
					// Save food place cuisine
					if($this->cboCuisine1Selector->SelectedValue > 0) 
					{
						Common::saveFoodPlaceCuisine(1,$foodPlaceRecord->ID,$this->cboCuisine1Selector->SelectedValue,$user->ID);						
					}
					
					if($this->cboCuisine2Selector->SelectedValue > 0) 
					{
						Common::saveFoodPlaceCuisine(2,$foodPlaceRecord->ID,$this->cboCuisine2Selector->SelectedValue,$user->ID);						
					}
					
					if($this->cboCuisine3Selector->SelectedValue > 0) 
					{
						Common::saveFoodPlaceCuisine(3,$foodPlaceRecord->ID,$this->cboCuisine3Selector->SelectedValue,$user->ID);						
					}
								
					$this->Response->redirect($this->Service->ConstructUrl('admin.EditFoodPlace', array('fid'=>$foodPlaceRecord->ID, 'm'=>'cfp')));			
					
				}
				else
				{					
					$this->Notice->Type = UserNoticeType::Error;
					$this->Notice->Text = $this->Application->getModule('message')->translate('ITEM_NOT_FOUND','user');
				}
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
	}

	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("admin.AdminMain"));				
	}
}

?>